<?php

/*
 * B端操作功能模块
 * 功能：公司
 */

namespace Home\Controller;

use Think\Controller;

class CompanyController extends Controller {

	public function __construct() {
        parent::__construct();
        $this->p = D('Passport', 'Logic');
        if(!$this->p->isLogin()){
            redirect("/");
        } 
        $this->c = D('Company','Logic');      
        $this->user_id = $_SESSION['id'];
    }

	public function index(){
		$company = $this->c->getCompanyByUserId();
        if (count($company) < 1) {
            $a = D('Area', 'Logic');
            $privinces = $a->getProvinces();
            $this->assign('provinces', $privinces);
            $this->display('create');
        } else {
            $this->assign('company', $company);
            $this->display('index');
        }	
	}

	public function submit(){
		$data['id']  = $_POST['id'];
		$data['user_id'] = $this->user_id;
		$data['name'] = $_POST['name'];
		$data['address'] = $_POST['address'];
		$data['tel'] = $_POST['tel'];
		$data['person'] = $_POST['person'];
		$data['intro'] = $_POST['intro'];
		$data['logo'] = $_POST['banner_logo'];
		if( strlen($data['name']) <1 
			|| strlen($data['address']) <1
			|| strlen($data['tel']) <1 
			|| strlen($data['person']) <1 
			|| strlen($data['intro']) <1 ){
			redirect('/error/?msg=');
		}
		if($data['id']>0){
			$re = $this->c->updateCompany($data);
			if($re){
				redirect('/home/company');
			}else{
				redirect('/error/?msg=');
			}
		}else{
			$data['create_time'] = time();
			$re = $this->c->insterCompany($data);
			if($re){
				redirect('/home/company');
			}else{
				redirect('/error/?msg=');
			}
		}
	}
	public function edit(){
		$data['id'] = $_GET['id'];
		$company = $this->c->getCompanyById($data['id']);
		$this->assign('company',$company);
		$this->display('create');
	}

	public function img(){
        layout(false);
        $this->display('img');
    }
    
    public function upload(){ 
        layout(false);
        import('Think.upload');
        $u = new \Think\Upload();
        $path = $u->upload($_FILES);
        $this->assign('path','/public/upload/'.$path['img']['savepath'].$path['img']['savename']);
        $this->display('img');
    }

} 